//
//  PhotoBrowserPresentationController.swift
//  WBDemo
//
//  Created by Meng on 17/2/22.
//  Copyright © 2017年 demo. All rights reserved.
//

import UIKit

protocol PhotoBrowserPresentationControllerDelegate: NSObjectProtocol {
    /// 用于创建一个和点击图片一模一样的UIImageView
    func browserPresentationWillShowImageView(browserPresenationController: PhotoBrowserPresentationController, indexPath: IndexPath) -> UIImageView?
    
    /// 用于获取点击图片相对于window的frame
    func browserPresentationWillFromFrame(browserPresenationController: PhotoBrowserPresentationController, indexPath: IndexPath) -> CGRect
    
    /// 用于获取点击图片最终的frame
    func browserPresentationWillToFrame(browserPresenationController: PhotoBrowserPresentationController, indexPath: IndexPath) -> CGRect
}

class PhotoBrowserPresentationController: UIPresentationController, UIViewControllerTransitioningDelegate, UIViewControllerAnimatedTransitioning {
    
    var isPresent = false // 是否展现
    
    weak var browserDelegate: PhotoBrowserPresentationControllerDelegate?
    var indexPath: IndexPath?
    
    // MARK: - UIViewControllerTransitioningDelegate
    // 该方法返回一个负责转场动画的对象
    func presentationController(forPresented presented: UIViewController, presenting: UIViewController?, source: UIViewController) -> UIPresentationController? {
        return PhotoBrowserPresentationController(presentedViewController: presented, presenting: presenting)
    }
    
    // 该方法返回一个负责转场如何出现的对象
    func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        isPresent = true
        return self
    }
    
    // 该方法返回一个负责转场如何消失的对象
    func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        isPresent = false
        return self
    }
    // MARK: - UIViewControllerAnimatedTransitioning
    // 告诉系统展现和消失的动画时长
    func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
        return 0.5
    }
    
    // 用于管理modal如何展现和消失
    func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
        
        if isPresent { // 展现
            guard let toView = transitionContext.view(forKey: .to) else {
                return
            }
            guard let bDelegate = browserDelegate else {
                return
            }
            guard let index = indexPath else {
                return
            }
            guard let imageView = bDelegate.browserPresentationWillShowImageView(browserPresenationController: self, indexPath: index) else {
                return
            }
            imageView.contentMode = UIViewContentMode.scaleAspectFill
            imageView.frame = bDelegate.browserPresentationWillFromFrame(browserPresenationController: self, indexPath: index)
            transitionContext.containerView.addSubview(imageView)
            
            UIView.animate(withDuration: transitionDuration(using: transitionContext), animations: {
                imageView.frame = bDelegate.browserPresentationWillToFrame(browserPresenationController: self, indexPath: index)
            }) { (_) in
                imageView.removeFromSuperview()
                transitionContext.containerView.addSubview(toView)
                // 告诉系统动画执行完毕了
                transitionContext.completeTransition(true)
            }
        } else { // 消失
//            guard let fromView = transitionContext.view(forKey: .from) else {
//                return
//            }
//            UIView.animate(withDuration: transitionDuration(using: transitionContext), animations: {
//                fromView.transform = CGAffineTransform.init(scaleX: 1.0, y: 0.00001)
//            }, completion: { (_) in
                transitionContext.completeTransition(true)
//            })
        }
        
    }
}
